package com.wantup.config;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * <p>
 *
 * </p>
 *
 * @author 赵洛伟
 * @since 2019-06-05
 * <p>
 * 第以数据库的配置信息
 */
@Configuration // 注册到springboot容器中
//@ConfigurationProperties(prefix = "spring.datasource.test1")
@Slf4j
@MapperScan(basePackages = "com.wantup.multi_datasource", sqlSessionFactoryRef = "test1SqlSessionFactory")
public class DataSource1Config {

    @Value("${spring.datasource.test1.jdbc-url}")
    private String dbUrl;

    @Value("${spring.datasource.test1.username}")
    private String username;

    @Value("${spring.datasource.test1.password}")
    private String password;

    @Value("${spring.datasource.test1.driver-class-name}")
    private String driverClassName;

    @Value("${spring.datasource.test1.borrowConnectionTimeout}")
    private int borrowConnectionTimeout;

    @Value("${spring.datasource.test1.loginTimeout}")
    private int loginTimeout;

    @Value("${spring.datasource.test1.maintenanceInterval}")
    private int maintenanceInterval;

    @Value("${spring.datasource.test1.maxIdleTime}")
    private int maxIdleTime;

    @Value("${spring.datasource.test1.maxLifetime}")
    private int maxLifetime;

    @Value("${spring.datasource.test1.maxPoolSize}")
    private int maxPoolSize;

    @Value("${spring.datasource.test1.minPoolSize}")
    private int minPoolSize;




    /**
     * 配置第一个数据库
     *
     * @return
     */
    @Bean(name = "test1DataSource")
    @Primary
    public DataSource testDataSource() throws SQLException {
        MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
        mysqlXaDataSource.setUrl(dbUrl);
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
        mysqlXaDataSource.setPassword(password);
        mysqlXaDataSource.setUser(username);
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);

        AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
        xaDataSource.setXaDataSource(mysqlXaDataSource);
        xaDataSource.setUniqueResourceName("test1DataSource");

        xaDataSource.setMinPoolSize(minPoolSize);
        xaDataSource.setMaxPoolSize(maxPoolSize);
        xaDataSource.setMaxLifetime(maxLifetime);
        xaDataSource.setBorrowConnectionTimeout(borrowConnectionTimeout);
        xaDataSource.setLoginTimeout(loginTimeout);
        xaDataSource.setMaintenanceInterval(maintenanceInterval);
        xaDataSource.setMaxIdleTime(maxIdleTime);
        return xaDataSource;
        //return DataSourceBuilder.create().type().build();
    }

    /**
     * 创建 第一个数据库的 SessionFactory
     *
     * @param dataSource
     * @return
     * @throws Exception
     */
    @Bean(name = "test1SqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        // bean.setMapperLocations(
        // new
        // PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
        return bean.getObject();
    }


    @Bean(name = "test1SqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }


}
